package com.hisense.ovcloud.mqtt.router.service;

import com.hisense.ovcloud.commons.dto.common.mqtt.MqttDeviceUploadMsg;
import com.hisense.ovcloud.commons.dto.req.MqttThirdPartyApiReq;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@Service
@Slf4j
public class ThirdPartyApi {

    private final WebClient webClient;

    public ThirdPartyApi(@Qualifier("base") WebClient webClient) {
        this.webClient = webClient;
    }

    public Mono<Boolean> notifyThirdPartyApi(MqttDeviceUploadMsg mqttDeviceUploadMsg){
        if(StringUtils.isNotEmpty(mqttDeviceUploadMsg.getDeviceUplinkUrl())){
            String baseUrl = mqttDeviceUploadMsg.getDeviceUplinkUrl();
            if(mqttDeviceUploadMsg.getDeviceUplinkUrl().contains("?")){
                baseUrl = mqttDeviceUploadMsg.getDeviceUplinkUrl().split("\\?")[0] + mqttDeviceUploadMsg.getEvent();
            }else{
                baseUrl = baseUrl + mqttDeviceUploadMsg.getEvent();
            }
            return webClient.post()
                    .uri(baseUrl)
                    .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                    .header("X-Zc-Msg-Attr_event", mqttDeviceUploadMsg.getOriginMsgType())
                    .header("X-Zc-Msg-Attr_token", mqttDeviceUploadMsg.getToken())
                    .header("X-Zc-Timeout", "600")
                    .header("X-Zc-Timestamp", String.valueOf(System.currentTimeMillis() / 1000))
                    .header("X-Zc-Nonce", mqttDeviceUploadMsg.getToken())
                    .header("X-Zc-Developer-Id", String.valueOf(mqttDeviceUploadMsg.getDeveloper_id()))
                    .header("X-Zc-Major-Domain", mqttDeviceUploadMsg.getDomain())
                    .header("X-Zc-Sub-Domain", mqttDeviceUploadMsg.getSubDomain())
                    .header("X-Zc-Access-Key", mqttDeviceUploadMsg.getAesKey())
                    .body(Mono.just(
                            MqttThirdPartyApiReq.builder()
                                    .err_description("")
                                    .is_parse_err(0)
                                    .payload(mqttDeviceUploadMsg.getPayload())
                                    .majorDomainId(Integer.parseInt(mqttDeviceUploadMsg.getDomain()))
                                    .subDomainId(Integer.parseInt(mqttDeviceUploadMsg.getSubDomain()))
                                    .physicalId(mqttDeviceUploadMsg.getWifiId())
                                    .payloadFormat(mqttDeviceUploadMsg.getPayloadFormat())
                                    .build()), MqttThirdPartyApiReq.class)
                    .retrieve()
                    .toEntity(String.class)
                    .flatMap(res -> {
                        if(org.apache.commons.lang3.StringUtils.isNotEmpty(res.getBody()) && res.getBody().contains("success")){
                            return Mono.just(true);
                        }else{
                            return  Mono.just(false);
                        }
                    });
        }else{
            return Mono.just(false);
        }
    }
}
